﻿#ifndef __COMMON_H__
#define __COMMON_H__

#include "ZMemState.h"

//依赖库头文件
#include <stdint.h>
#include <string.h>
#include <string>

#include "base/RefBase.h"
#include "base/sigslot.h"
#include "SDL2/include/SDL.h"
#include "tinyxml2/tinyxml2.h"
#include "ZLog.h"

#ifdef WIN32
#include <assert.h>
#endif

#include "support/stb_image.h"

#undef DrawText

//使用nedmalloc
#ifdef USE_NED_MALLOC
#include "malloc/nedmalloc.h"
#define zmalloc nedmalloc
#define zfree nedfree
#define zrealloc nedrealloc
#else
#define zmalloc malloc
#define zrealloc realloc
#define zfree free
#endif

namespace ZuiLib
{
	class ZString;
	typedef uint32_t ZWChar;
	typedef char	ZChar;

	template<class T>
	inline void zswap(T& v1,T& v2)
	{
		T temp=v1;
		v1=v2;
		v2=temp;
	}

	template<class T>
	inline T zmin(const T& v1,const T& v2)
	{
		if (v2<v1)
			return v2;
		else
			return v1;
	}

	template<class T>
	inline T zmax(const T& v1, const T& v2)
	{
		if (v1<v2)
			return v2;
		else
			return v1;
	}

	inline char* AllocString(const char* str)
	{
		int len=strlen(str)+1;
		char* ptr=(char* )zmalloc(len);
		memcpy(ptr,str,len);
		return ptr;
	}

	inline void FreeString(char* str)
	{
		zfree(str);
	}


#ifndef DISALLOW_ASSIGN
	#define DISALLOW_ASSIGN(TypeName) \
	  void operator=(const TypeName&)
#endif


// A macro to disallow the evil copy constructor and operator= functions
// This should be used in the private: declarations for a class
#ifndef DISALLOW_COPY_AND_ASSIGN
	#define DISALLOW_COPY_AND_ASSIGN(TypeName)    \
	  TypeName(const TypeName&);                    \
	  DISALLOW_ASSIGN(TypeName)
#endif


// Alternative, less-accurate legacy name.
#ifndef DISALLOW_EVIL_CONSTRUCTORS
	#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
	  DISALLOW_COPY_AND_ASSIGN(TypeName)
#endif

// A macro to disallow all the implicit constructors, namely the
// default constructor, copy constructor and operator= functions.
//
// This should be used in the private: declarations for a class
// that wants to prevent anyone from instantiating it. This is
// especially useful for classes containing only static methods.

#ifndef DISALLOW_IMPLICIT_CONSTRUCTORS
	#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
	  TypeName();                                    \
	  DISALLOW_EVIL_CONSTRUCTORS(TypeName)
#endif

	//utf8和unicode转换
	int ZTextCharFromUtf8(ZWChar* out_char, const char* in_text, const char* in_text_end);
	int ZTextStrFromUtf8(ZWChar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining);
	int ZTextCountCharsFromUtf8(const char* in_text, const char* in_text_end);
	int ZTextStrToUtf8(char* buf, int buf_size, const ZWChar* in_text, const ZWChar* in_text_end);
	int ZTextCountUtf8BytesFromStr(const ZWChar* in_text, const ZWChar* in_text_end);

#ifdef WIN32
    std::wstring Utf8ToUnicode(const std::string& str);
    std::string UnicodeToUtf8(const std::wstring& str);
#endif

#ifdef WIN32
#define ZASSERT _ASSERT
#else
#define ZASSERT SDL_assert
#define stricmp strcasecmp
#endif

	//hash算法
	uint32_t zhash(register const unsigned char *k,uint32_t length,uint32_t initval=0x46A2F8E9);

	inline Uint32 ZGetTickCount(){ return SDL_GetTicks();}

	//根据字符串生成hash字符串
	ZString zstrhash(const char* text,int len=-1);


#define ZARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))


#define ZOVERRIDE override

}//namespace






#endif
